home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Language / Compiler / writeAT.c < prev    next >
C/C++ Source or Header  |  1990-08-16  |  3KB  |  154 lines

  1. /*
  2.  * @(#)writeAT.c    1.5  7/1/87
  3.  */
  4. #include "assert.h"
  5. #include "nodes.h"
  6. #include "sequence.h"
  7. #include "system.h"
  8. #include "builtins.h"
  9.  
  10. static char *vlbBuffer = NULL;
  11. static int vlbBufferLength;
  12. static char *addPoint;
  13. static Boolean doExpand = FALSE;
  14. static Boolean doTypesOK = FALSE;
  15.  
  16. void vlbStart()
  17. {
  18.   vlbBufferLength = 100;
  19.   vlbBuffer = malloc((unsigned)vlbBufferLength);
  20.   addPoint = vlbBuffer;
  21. }
  22.  
  23. void vlbAppend(s)
  24. char *s;
  25. {
  26.   register int length = strlen(s);
  27.   if (length + addPoint >= vlbBuffer + vlbBufferLength) {
  28.     char *nb;
  29.     vlbBufferLength *= 2;
  30.     nb = malloc((unsigned)vlbBufferLength);
  31.     strcpy(nb, vlbBuffer);
  32.     addPoint = nb + (addPoint - vlbBuffer);
  33.     free(vlbBuffer);
  34.     vlbBuffer = nb;
  35.   }
  36.   strcpy(addPoint, s);
  37.   addPoint += length;
  38. }
  39.  
  40. char *vlbDone()
  41. {
  42.   return(vlbBuffer);
  43. }
  44.  
  45. static NodePtr me = NULL;
  46.  
  47. Boolean isMe(p)
  48. NodePtr p;
  49. {
  50.   return(p == me);
  51. }
  52.  
  53. Boolean writeOID(id)
  54. OID id;
  55. {
  56.   char buf[12];
  57.   if (id == 0) return(FALSE);
  58.   sprintf(buf, "O%08x", id);
  59.   vlbAppend(buf);
  60.   return(TRUE);
  61. }
  62.  
  63. extern OID getID();
  64. extern NodePtr 
  65.     findManifestValue(),
  66.     figureOutAT();
  67.  
  68. static Boolean writeParams(p, doingResults)
  69. register NodePtr p;
  70. Boolean doingResults;
  71. {
  72.   register NodePtr q, r, t;
  73.   OID id;
  74.   vlbAppend("[");
  75.   Sequence_For(q, p)
  76.     assert(q->tag == P_PARAM);
  77.     r = q->b.param.sym->b.symdef.symbol->value.ATinfo;
  78.     if (r == NN) {
  79.       /*
  80.        * We may not have assigned types yet, so look at the type expression.
  81.        */
  82.       r = findManifestValue(q->b.param.type);
  83.       if (r != NN) {
  84.     r = figureOutAT(r);
  85.       }
  86.       if (r == NN) {
  87.     /*
  88.      * We have something that is not a type here.  Just write garbage
  89.      * and return false.
  90.      */
  91.     vlbAppend("JUNK");
  92.     return(FALSE);
  93.       }
  94.     }
  95.     if (! doExpand && isMe(r)) {
  96.       vlbAppend("S");
  97.     } else {
  98.       t = GETVALUE(r);
  99.       assert(t->tag == P_ATLIT);
  100.       if (t->b.atlit.f.dependsOnTypeVariable) {
  101.     if (!doTypesOK) return(FALSE);
  102.     if (doingResults) {
  103.       id = OIDOfBuiltin(B_INSTAT, NILINDEX);
  104.     } else {
  105.       id = OIDOfBuiltin(B_INSTAT, ANYINDEX);
  106.     }
  107.       } else {
  108.     id = t->b.atlit.id;
  109.       }
  110.       if (!writeOID(id)) return(FALSE);
  111.     }
  112.     if (z__z < p->nChildren-1) vlbAppend(",");
  113.   Sequence_Next
  114.   vlbAppend("]");
  115.   return(TRUE);
  116. }
  117.  
  118. static Boolean writeOp(p)
  119. NodePtr p;
  120. {
  121.   assert(p->tag == P_OPSIG);
  122. /*  assert(p->b.opsig.where == NULL); */
  123.   vlbAppend(p->b.opsig.isFunction ? "F" : "O");
  124.   if (!writeOID(p->b.opsig.name->b.opname.id)) return(FALSE);
  125.   if (!writeParams(p->b.opsig.params, FALSE)) return(FALSE);
  126.   vlbAppend("->");
  127.   if (!writeParams(p->b.opsig.results, TRUE)) return(FALSE);
  128.   return(TRUE);
  129. }
  130.  
  131. char *writeAT(p, expand, typesOK)
  132. register NodePtr p;
  133. Boolean expand, typesOK;
  134. {
  135.   register NodePtr q;
  136.   assert(! p->b.atlit.f.isTypeVariable);
  137.   assert(! p->b.atlit.f.dependsOnTypeVariable);
  138.   assert(p->b.atlit.f.isManifest);
  139.   doExpand = expand;
  140.   doTypesOK = typesOK;
  141.   me = p;
  142.   vlbStart();
  143.   vlbAppend(p->b.atlit.f.cannotBeConformedTo ? "Y" : "N");
  144.   vlbAppend(p->b.atlit.f.isVector ? "Y" : "N");
  145.   vlbAppend(p->b.atlit.f.immutable ? "Y" : "N");
  146.   Sequence_For(q, p->b.atlit.ops)
  147.     if (!writeOp(q)) {
  148.       return(NULL);
  149.     }
  150.   Sequence_Next
  151.   vlbAppend("\n");
  152.   return(vlbDone());
  153. }
  154.